﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace KDSugar.Galaxy.API.AuditOrUnAudit
{
    /// <summary>
    /// 单据审核操作指令
    /// </summary>
    public class AuditCommand : Base.BaseNumberIdNetwork
    {
        /// <summary>
        /// 单据审核操作指令URL
        /// </summary>
        public const string AUDIT_COMMAND_URL = "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc";

        /// <summary>
        /// 单据审核操作指令初始化
        /// </summary>
        /// <param name="_formId"></param>
        /// <param name="_client"></param>
        public AuditCommand(string _formId,GalaxyCloudAPIClient _client)
            : base(AUDIT_COMMAND_URL, _formId, _client)
        {
            
        }

        /// <summary>
        /// （仅用作于反审核指令实现的继承构造）
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="_formId"></param>
        /// <param name="_client"></param>
        protected AuditCommand(string _url,string _formId,GalaxyCloudAPIClient _client)
            : base(_url, _formId, _client)
        {

        }

        /// <summary>
        /// 验证审核/反审核操作返回数据是否符合规则
        /// </summary>
        /// <param name="_responseData"></param>
        /// <returns></returns>
        public override bool ValidateResponseDataRule(string _responseData)
        {
            //bool result = false;
            //if (!string.IsNullOrWhiteSpace(_responseData))
            //{
            //    try
            //    {
            //        Base.Result.BaseReturnInfo resultInfo = JsonConvert.DeserializeObject<Base.Result.BaseReturnInfo>(_responseData);
            //        if (resultInfo.Result != null)
            //        {
            //            result = true;
            //        }
            //    }
            //    catch
            //    {
            //        result = false;
            //    }
            //}
            //return result;
            return true;
        }

        /// <summary>
        /// 交互标志集合
        /// </summary>
        public string InterationFlags { get; private set; }

        /// <summary>
        /// 设置交互标志集合
        /// </summary>
        /// <param name="_flags">
        /// 交互标志集合
        /// <para>分号分隔，格式："flag1;flag2;..."</para>
        /// </param>
        public void SetInterationFlags(string _flags)
        {
            InterationFlags = _flags;
        }

        /// <summary>
        /// 设置交互标志集合
        /// </summary>
        /// <param name="_flags">交互标志集合</param>
        public void SetInterationFlags(List<string> _flags)
        {
            InterationFlags = Helper.ArraysToString(_flags);
        }

        /// <summary>
        /// 构建审核/反审核指令的请求内容
        /// </summary>
        /// <returns></returns>
        public override string BuildRequestContent()
        {
            return BuildRequestContentByAppendFormId();
        }

        /// <summary>
        /// 执行操作
        /// </summary>
        /// <returns></returns>
        public Base.Result.ResponseStatusInfo Execute()
        {
            Base.Result.ResponseStatusInfo result = null;
            var resultPack = APIClient.ExecuteCommand(this);
            if (resultPack != null)
            {
                string errorMsg = "未知错误";
                try
                {
                    if (ValidateResponseDataRule(resultPack.ResponseData))
                    {
                        errorMsg = "返回数据JSON反序列化转换对象失败";
                        Base.Result.BaseReturnInfo returnInfo = JsonConvert.DeserializeObject<Base.Result.BaseReturnInfo>(resultPack.ResponseData);
                        if (returnInfo != null)
                        {
                            var resultInfo = returnInfo.Result;
                            if (resultInfo != null)
                            {
                                result = resultInfo.ResponseStatus;
                                if (result == null)
                                {
                                    errorMsg = "返回状态信息ResponseStatus为NULL";
                                    throw new Exception(errorMsg);
                                }
                            }
                            else
                            {
                                errorMsg = "返回信息Result为NULL";
                                throw new Exception(errorMsg);
                            }
                        }
                        else
                        {
                            errorMsg = "反序列化对象后对象为NULL";
                            throw new Exception(errorMsg);
                        }
                    }
                    else
                    {
                        errorMsg = "返回数据规则验证为false";
                        throw new Exception(errorMsg);
                    }
                }
                catch(Except.ExecuteException ex)
                {
                    ex.OperateCommandName = GetOperateCommandName();
                    ex.AppendMessage = errorMsg;
                    ex.ExecuteData = resultPack;
                    throw ex;
                }
            }
            else
            {
                //未获取到返回
                throw new Exception(Constant.GetExecuteReturnNullErrorStr("审核/反审"));
            }
            return result;
        }
    }
}
